package com.example.course.service.impl;

import com.example.course.bean.Course;
import com.example.course.mapper.CourseMapper;
import com.example.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public List<Course> getCourse() {
        return courseMapper.selectCourse();
    }

    @Override
    public int updateCourse(Course course) {
        return courseMapper.updateCourse(course);
    }

    @Override
    public int deleteCourse(int id) {
        int i = courseMapper.deleteCourse(id);
        if (i == 1) {
            if (courseMapper.selectStudentByCourseId(id) != null && courseMapper.selectTeacherByCourseId(id)!=null) {
                int i1 = courseMapper.deleteCourseStudent(id);
                int i2 = courseMapper.deleteCourseTeacher(id);
                if (i1!= 1 && i2 != 0) {
                    System.out.println(2);
                    return 1;
                }else {
                    System.out.println(3);
                    return 0;
                }
            }else if (courseMapper.selectStudentByCourseId(id) != null){
                int i1 = courseMapper.deleteCourseStudent(id);
                if (i1 != 1) {
                    return 1;
                }else {
                    return 0;
                }
            }else if (courseMapper.selectTeacherByCourseId(id)!=null) {
                int i2 = courseMapper.deleteCourseTeacher(id);

                if (i2 != 1) {
                    return 1;
                }else {
                    return 0;
                }
            }else {
                return i;
            }
        }
        return 0;
    }

    @Override
    public int insertCourse(Course course, int id) {
        int i = courseMapper.insertCourse(course);
        if(i!=0){
            System.out.println(2);
            int f = courseMapper.insertCourseById(id,course.getId());
            return f;
        }
        return 0;
    }

    @Override
    public List<Course> getCourseList(int id) {
        return courseMapper.selectCourseByTeacherId(id);
    }

    @Override
    public Course getCourseById(int id) {
        return courseMapper.selectCourseById(id);
    }

    @Override
    public List<Course> getCourseByStudentId(int id) {
        return courseMapper.selectCourseByStudentId(id);
    }

}
